/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;
using DaveSexton.DocProject.Resources;

namespace DaveSexton.DocProject.Engine
{
	public class ChangeCurrentDirectoryBuildStep<TEngine> : BuildStep<TEngine>
		where TEngine : IBuildEngine
	{
		#region Public Properties
		public override string Name
		{
			get
			{
				return "Change Directory";
			}
		}

		public string Directory
		{
			get
			{
				return directory;
			}
		}
		#endregion

		#region Private / Protected
		private readonly string directory;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ChangeCurrentDirectoryBuildStep" /> class.
		/// </summary>
		public ChangeCurrentDirectoryBuildStep(TEngine engine, string directory)
			: base(engine)
		{
			if (string.IsNullOrEmpty(directory))
				throw new ArgumentException(Errors.NullOrEmpty, "directory");

			this.directory = directory;
		}
		#endregion

		#region Methods
		protected override void ExecuteInternal(BuildContext context)
		{
			context.Invoke(delegate()
			{
				context.TraceLine();
				context.TraceLine("Changing current directory from \"{0}\" to \"{1}\"", context.CurrentDirectory, directory);
			});

			context.CurrentDirectory = directory;
		}

		protected override void CancelInternal(BuildContext context)
		{
			// do nothing
		}

		public override string ToString()
		{
			return "Change current directory to \"" + directory + "\".";
		}
		#endregion
	}
}
